﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HtmlAgilityPack;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Mvc.RazorTools.Glyphicons.Tests
{
	[TestClass]
	public class GlyphiconTests
	{
		[TestMethod]
		[TestCategory("Glyphicon")]
		public void GlyphiconExpectedIconCountTest()
		{
			Assert.AreEqual(GlyphiconsSet.IconCount, GlyphiconsSet.AllItems.Count());
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void GlyphiconConstructorTest1()
		{
			Glyphicon icon = new Glyphicon();
			Assert.AreEqual(string.Empty, icon.Name);
			Assert.AreEqual(false, icon.Locked);
			Assert.AreEqual(string.Empty, icon.IconId);
			Assert.AreEqual("i", icon.HtmlTag);
			Assert.AreEqual("<i></i>", icon.Html.ToString());
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void GlyphiconConstructorTest2()
		{
			Glyphicon icon = new Glyphicon("Category", "icon-test");
			Assert.AreEqual("Category", icon.Category);
			Assert.AreEqual("icon-test", icon.Name);
			Assert.AreEqual(false, icon.Locked);
			Assert.AreEqual("icon-test", icon.IconId);
			Assert.AreEqual("i", icon.HtmlTag);

			HtmlNode node = icon.HtmlNode();
			Assert.AreEqual(true, node.Attributes.Contains("class"));
			Assert.AreEqual(true, node.Attributes["class"].Value.Contains(icon.IconId));
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void ImplicitCastToMvcHtmlString()
		{
			Glyphicon icon = GlyphiconsSet.Free.Star;
			MvcHtmlString s = icon;
			Assert.AreEqual(icon.Html.ToString(), s.ToString());
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void ImplicitCastToString()
		{
			Glyphicon icon = GlyphiconsSet.Free.Star;
			string s = icon;
			Assert.AreEqual(icon.Html.ToString(), s);
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void CreateTest1()
		{
			Glyphicon icon = Glyphicon.Create("Category", "icon-test");
			Assert.AreEqual("Category", icon.Category);
			Assert.AreEqual("icon-test", icon.Name);
			Assert.AreEqual(false, icon.Locked);
			Assert.AreEqual("icon-test", icon.IconId);
			Assert.AreEqual("i", icon.HtmlTag);

			HtmlNode node = icon.HtmlNode();
			Assert.AreEqual(true, node.Attributes.Contains("class"));
			Assert.AreEqual(true, node.Attributes["class"].Value.Contains(icon.IconId));
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void CreateTest2()
		{
			Glyphicon icon = Glyphicon.Create("Category", "icon-test", "Test");
			Assert.AreEqual("Category", icon.Category);
			Assert.AreEqual("Test", icon.Name);
			Assert.AreEqual(false, icon.Locked);
			Assert.AreEqual("icon-test", icon.IconId);
			Assert.AreEqual("i", icon.HtmlTag);

			HtmlNode node = icon.HtmlNode();
			Assert.AreEqual(true, node.Attributes.Contains("class"));
			Assert.AreEqual(true, node.Attributes["class"].Value.Contains(icon.IconId));
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void CreateTest4()
		{
			Dictionary<string, string> classAttributes = new Dictionary<string, string>();
			classAttributes.Add("item1", "value1");
			classAttributes.Add("item2", "value2");
			classAttributes.Add("item3", "value3");

			Glyphicon icon = Glyphicon.Create("Category", "icon-test", classAttributes);
			Assert.AreEqual("Category", icon.Category);
			Assert.AreEqual("icon-test", icon.Name);
			Assert.AreEqual(false, icon.Locked);
			Assert.AreEqual("icon-test", icon.IconId);
			Assert.AreEqual("i", icon.HtmlTag);

			HtmlNode node = icon.HtmlNode();
			Assert.AreEqual(true, node.Attributes.Contains("class"));
			Assert.AreEqual(true, node.Attributes["class"].Value.Contains(icon.IconId));
			Assert.AreEqual(true, node.Attributes["class"].Value.Contains("value1"));
			Assert.AreEqual(true, node.Attributes["class"].Value.Contains("value1"));
			Assert.AreEqual(true, node.Attributes["class"].Value.Contains("value1"));
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void CreateTest6()
		{
			Glyphicon icon = Glyphicon.Create("Category", "icon-test", "Test", true);
			Assert.AreEqual("Category", icon.Category);
			Assert.AreEqual("Test", icon.Name);
			Assert.AreEqual(true, icon.Locked);
			Assert.AreEqual("icon-test", icon.IconId);
			Assert.AreEqual("i", icon.HtmlTag);

			HtmlNode node = icon.HtmlNode();
			Assert.AreEqual(true, node.Attributes.Contains("class"));
			Assert.AreEqual(true, node.Attributes["class"].Value.Contains(icon.IconId));
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void CloneTest1()
		{
			Glyphicon icon1 = new Glyphicon("Category", "icon-test");
			object icon2 = icon1.Clone();

			Assert.IsInstanceOfType(icon2, typeof(Glyphicon));
			Assert.AreNotSame(icon1, icon2);
			Assert.AreEqual("Category", ((Glyphicon)icon2).Category);
			Assert.AreEqual("icon-test", ((Glyphicon)icon2).Name);
			Assert.AreEqual(false, ((Glyphicon)icon2).Locked);
			Assert.AreEqual("icon-test", ((Glyphicon)icon2).IconId);
			Assert.AreEqual("i", ((Glyphicon)icon2).HtmlTag);
			Assert.AreEqual(icon1.Html.ToString(), ((Glyphicon)icon2).Html.ToString());
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void CloneTest2()
		{
			Glyphicon icon1 = Glyphicon.Create("Category", "icon-test", "Test");
			object icon2 = icon1.Clone();

			Assert.IsInstanceOfType(icon2, typeof(Glyphicon));
			Assert.AreNotSame(icon1, icon2);
			Assert.AreEqual("Category", ((Glyphicon)icon2).Category);
			Assert.AreEqual("Test", ((Glyphicon)icon2).Name);
			Assert.AreEqual(false, ((Glyphicon)icon2).Locked);
			Assert.AreEqual("icon-test", ((Glyphicon)icon2).IconId);
			Assert.AreEqual("i", ((Glyphicon)icon2).HtmlTag);
			Assert.AreEqual(icon1.Html.ToString(), ((Glyphicon)icon2).Html.ToString());
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void CloneLockedItemTest()
		{
			Glyphicon icon1 = Glyphicon.Create("Category", "icon-test", "icon-test", true);
			icon1.ClassAttributes.Add("item1", "value1");
			icon1.ClassAttributes.Add("item2", "value2");
			icon1.ClassAttributes.Add("item3", "value3");
			object icon2 = icon1.Clone();

			Assert.IsInstanceOfType(icon2, typeof(Glyphicon));
			Assert.AreNotSame(icon1, icon2);
			Assert.AreEqual("Category", ((Glyphicon)icon2).Category);
			Assert.AreEqual("icon-test", ((Glyphicon)icon2).Name);
			Assert.AreEqual(false, ((Glyphicon)icon2).Locked);
			Assert.AreEqual("icon-test", ((Glyphicon)icon2).IconId);
			Assert.AreEqual("i", ((Glyphicon)icon2).HtmlTag);
			Assert.AreEqual(icon1.Html.ToString(), ((Glyphicon)icon2).Html.ToString());
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void SetIdTest()
		{
			Glyphicon icon = new Glyphicon();
			icon.IconId = "icon-test";
			Assert.AreEqual("icon-test", icon.Name);
			Assert.AreEqual(false, icon.Locked);
			Assert.AreEqual("icon-test", icon.IconId);
			Assert.AreEqual("i", icon.HtmlTag);

			HtmlNode node = icon.HtmlNode();
			Assert.AreEqual(true, node.Attributes.Contains("class"));
			Assert.AreEqual(true, node.Attributes["class"].Value.Contains(icon.IconId));
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		[ExpectedException(typeof(ModifyLockedInstanceException))]
		public void SetIdWhenLockedTest()
		{
			Glyphicon icon = Glyphicon.Create("Category", "icon-test", "Test", true);
			icon.IconId = "icon-something-else";
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		[ExpectedException(typeof(ModifyLockedInstanceException))]
		public void SetNameWhenLockedTest()
		{
			Glyphicon icon = Glyphicon.Create("Category", "icon-test", "Test", true);
			icon.Name = "OtherName";
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		[ExpectedException(typeof(ModifyLockedInstanceException))]
		public void SetUnlockedWhenLockedTest()
		{
			Glyphicon icon = Glyphicon.Create("Category", "icon-test", "Test", true);
			icon.Locked = false;
		}

		[TestMethod]
		[TestCategory("Glyphicon")]
		public void SetHtmlTagTest()
		{
			Glyphicon icon = Glyphicon.Create("Category", "icon-test");
			icon.HtmlTag = "span";

			HtmlNode node = icon.HtmlNode();
			Assert.AreEqual(icon.HtmlTag, node.Name);
		}
	}
}
